/******************************************************************************
 * Copyright (C) 2010 ShenZhen ComTop Information Technology Co.,Ltd
 * All Rights Reserved.
 * 本软件为深圳康拓普开发研制。未经本公司正式书面同意，其他任何个人、团体不得使用、
 * 复制、修改或发布本软件.
 *****************************************************************************/

package com.code.common.commonpool;

import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.pool.impl.GenericObjectPool;

/**
 * 池工厂类
 * 
 * @author 郑重
 * @since 1.0
 * @version 2010-10-27 郑重
 */
public class ObjectPool {
    
    /**
     * 构建池对象
     */
    private GenericObjectPool genericObjectPool;
    
    /**
     * 池工厂
     */
    private PoolableObjectFactory poolableObjectFactory;
    
    /**
     * 最大活动线程
     */
    private int maxActive = 100;
    
    /**
     * 当池为空时策略
     */
    private byte whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
    
    /**
     * 最大等待时间
     */
    private long maxWait = 3600;
    
    /**
     * 最大空闲对象
     */
    private int maxIdle = 20;
    
    /**
     * 出池时是否测试
     */
    private boolean testOnBorrow = true;
    
    /**
     * 回收时是否测试对象
     */
    private boolean testOnReturn = true;
    
    /**
     * Pool初始化
     */
    public void init() {
        genericObjectPool =
            new GenericObjectPool(poolableObjectFactory, maxActive, whenExhaustedAction, maxWait, maxIdle, true, true);
    }
    
    /**
     * Pool销毁
     */
    public void destroy() {
        try {
            genericObjectPool.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 回收对象
     */
    public void clear() {
        genericObjectPool.clear();
    }
    
    /**
     * 取出后对象
     * 
     * @return 获取连接
     * @throws Exception 异常
     */
    public Object borrowObject() throws Exception {
        return genericObjectPool.borrowObject();
    }
    
    /**
     * 回收对象
     * 
     * @param returnObject 返回对象
     * @throws Exception 异常
     */
    public void returnObject(Object returnObject) throws Exception {
        genericObjectPool.returnObject(returnObject);
    }
    
    /**
     * @return 获取 genericObjectPool属性值
     */
    public GenericObjectPool getGenericObjectPool() {
        return genericObjectPool;
    }
    
    /**
     * @return 获取 maxActive属性值
     */
    public int getMaxActive() {
        return maxActive;
    }
    
    /**
     * @return 获取 maxIdle属性值
     */
    public int getMaxIdle() {
        return maxIdle;
    }
    
    /**
     * @return 获取 maxWait属性值
     */
    public long getMaxWait() {
        return maxWait;
    }
    
    /**
     * @return 获取 poolableObjectFactory属性值
     */
    public PoolableObjectFactory getPoolableObjectFactory() {
        return poolableObjectFactory;
    }
    
    /**
     * @return 获取 whenExhaustedAction属性值
     */
    public byte getWhenExhaustedAction() {
        return whenExhaustedAction;
    }
    
    /**
     * @return 获取 testOnBorrow属性值
     */
    public boolean isTestOnBorrow() {
        return testOnBorrow;
    }
    
    /**
     * @return 获取 testOnReturn属性值
     */
    public boolean isTestOnReturn() {
        return testOnReturn;
    }
    
    /**
     * @param genericObjectPool 设置 genericObjectPool 属性值为参数值 genericObjectPool
     */
    public void setGenericObjectPool(GenericObjectPool genericObjectPool) {
        this.genericObjectPool = genericObjectPool;
    }
    
    /**
     * @param maxActive 设置 maxActive 属性值为参数值 maxActive
     */
    public void setMaxActive(int maxActive) {
        this.maxActive = maxActive;
    }
    
    /**
     * @param maxIdle 设置 maxIdle 属性值为参数值 maxIdle
     */
    public void setMaxIdle(int maxIdle) {
        this.maxIdle = maxIdle;
    }
    
    /**
     * @param maxWait 设置 maxWait 属性值为参数值 maxWait
     */
    public void setMaxWait(long maxWait) {
        this.maxWait = maxWait;
    }
    
    /**
     * @param poolableObjectFactory 设置 poolableObjectFactory 属性值为参数值 poolableObjectFactory
     */
    public void setPoolableObjectFactory(PoolableObjectFactory poolableObjectFactory) {
        this.poolableObjectFactory = poolableObjectFactory;
    }
    
    /**
     * @param testOnBorrow 设置 testOnBorrow 属性值为参数值 testOnBorrow
     */
    public void setTestOnBorrow(boolean testOnBorrow) {
        this.testOnBorrow = testOnBorrow;
    }
    
    /**
     * @param testOnReturn 设置 testOnReturn 属性值为参数值 testOnReturn
     */
    public void setTestOnReturn(boolean testOnReturn) {
        this.testOnReturn = testOnReturn;
    }
    
    /**
     * @param whenExhaustedAction 设置 whenExhaustedAction 属性值为参数值 whenExhaustedAction
     */
    public void setWhenExhaustedAction(byte whenExhaustedAction) {
        this.whenExhaustedAction = whenExhaustedAction;
    }
}